package com.xc.utils.stock;

import com.xc.utils.DateTimeUtil;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 *  得到持仓的天数
 * @author xiongcan
 * @version 1.0
 * @date 2018/10/5 下午5:52
 */
@Slf4j
public class GetStayDays {

    /**
     *  把 yyyy-MM-dd HH:mm:ss 的date 格式转换成开始的时间
     * @param beginDate
     * @return
     */
    public static Date getBeginDate(Date beginDate){
        String date = DateTimeUtil.dateToStr(beginDate,"yyyy-MM-dd");
        return DateTimeUtil.strToDate(date,"yyyy-MM-dd");
    }


    //  得到工作日到现在的时间(排除周末)
    public static int getWorkDays(Date StartDate) {
        Calendar cl1 = Calendar.getInstance();
        Calendar cl2 = Calendar.getInstance();

        Date begin_date = getBeginDate(StartDate);
        Date end_date = getBeginDate(new Date());

        try {
            cl1.setTime(begin_date);
            cl2.setTime(end_date);
        } catch (Exception e) {
            System.out.println("日期格式非法");
            e.printStackTrace();
        }
        int count = 0;
        while (cl1.compareTo(cl2) <= 0) {
            if (cl1.get(Calendar.DAY_OF_WEEK) != 7 && cl1.get(Calendar.DAY_OF_WEEK) != 1)
                count++;
            cl1.add(Calendar.DAY_OF_MONTH, 1);
        }
        return count - 1;
    }

    //  得到工作日到现在的时间(不排除周末)
    public static int getDays(Date StartDate) {
        Calendar cl1 = Calendar.getInstance();
        Calendar cl2 = Calendar.getInstance();

        try {
            cl1.setTime(StartDate);
            cl2.setTime(new Date());
        } catch (Exception e) {
            System.out.println("日期格式非法");
            e.printStackTrace();
        }
        int count = 0;
        while (cl1.compareTo(cl2) <= 0) {
            count++;
            cl1.add(Calendar.DAY_OF_MONTH, 1);
        }
        return count - 1;
    }


    //  测试工作日时间
    public static int testWorkDays(Date beginDate,Date endDate) {
        Calendar cl1 = Calendar.getInstance();
        Calendar cl2 = Calendar.getInstance();

        try {
            cl1.setTime(beginDate);
            cl2.setTime(endDate);
        } catch (Exception e) {
            System.out.println("日期格式非法");
            e.printStackTrace();
        }
        int count = 0;
        while (cl1.compareTo(cl2) <= 0) {
            if (cl1.get(Calendar.DAY_OF_WEEK) != 7 && cl1.get(Calendar.DAY_OF_WEEK) != 1)
                count++;
            cl1.add(Calendar.DAY_OF_MONTH, 1);
        }
        return count - 1;
    }


    public static void main(String[] args) throws Exception{
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date start = df.parse("2019-03-10");

        System.out.println("排除周末：" + getWorkDays(start));
        System.out.println("不排除周末： " + getDays(start));

        System.out.println(" -------- ");

        SimpleDateFormat df2= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date begindate = df2.parse("2019-03-10 11:39:09");
        Date enddate = df2.parse("2019-03-11 07:15:00");
        log.info("测试的天数 = {}",testWorkDays(begindate,enddate));
        log.info("测试的天数 = {}",getDays(begindate));


        System.out.println(DateTimeUtil.dateToStr(getBeginDate(begindate)));
        System.out.println(DateTimeUtil.dateToStr(getBeginDate(enddate)));

    }


}
